package com.xbb.kafka.producer;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;

public class CallbackProducer {
  public static void main(String[] args) {

    Properties properties = new Properties();

    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop1:9092");
    properties.put(ProducerConfig.ACKS_CONFIG, "all");
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

    KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

    for (int i = 0; i < 10; i++) {
      producer.send(new ProducerRecord<>(
              "top",
              "message--" + i
      ), (recordMetadata, e) -> {
        if (e == null) {
          System.out.println(recordMetadata.partition() + "--" + recordMetadata.offset());
        }
      });
    }

    producer.close();

  }
}
